local skel = fk.CreateSkill {
  name = "lb__guying",
}

local DIY = require "packages.diy_utility.diy_utility"

Fk:loadTranslationTable{
  ["lb__guying"] = "顾影",
  [":lb__guying"] = "出牌阶段限一次，你可以将本回合进入弃牌堆的牌以任意顺序置于牌堆顶，视为使用一张仅失去这些牌的角色可以参与选择的【五谷丰登】。",

  ["#lb__guying"] = "顾影：你可以将本回合进入弃牌堆的牌以任意顺序置于牌堆顶，视为使用一张仅失去这些牌的角色可以参与选择的【五谷丰登】",
  ["#lb__guying_sort"] = "顾影：请将这些牌以任意顺序置于牌堆顶",

  ["$lb__guying1"] = "持明的生命…就像一个个梦一样…",
  ["$lb__guying2"] = "不过也就一方小小的天地，却感觉可以比平时更加自由地呼吸。",
}

skel:addEffect("active", {
  anim_type = "drawcard",
  prompt = "#lb__guying",
  can_use = function (self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) < 1  and #DIY.getCenterCards(Fk:currentRoom()) > 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  on_use = function (self, room, skillUseEvent)
    local player = skillUseEvent.from
    --[[
    local cards = room:askToArrangeCards(player, {
      card_map = {DIY.getCenterCards(room)},
      skill_name = skel.name,
      default_choice = DIY.getCenterCards(room),
      prompt = "#lb__guying_sort",
      free_arrange = true,
    })[1]
    --]]
    local cards = DIY.getCenterCards(room)
    local n = #cards
    cards = room:askToGuanxing(player, {
      cards = cards,
      area_names = {"Top", "toObtain"},
      top_limit = {n, n},
      bottom_limit = {0, 0}, --为0不显示第二行
      skill_name = skel.name,
      prompt = "#lb__guying_sort",
      skip = true,
    }).top
    local tos = {}
    for _, id in ipairs(cards) do
      room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          for _, info in ipairs(move.moveInfo) do
            if move.from and info.cardId == id and (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) then
              table.insertIfNeed(tos, move.from.id)
            end
          end
        end
      end, Player.HistoryTurn)
    end
    --[[
    local list = player:getTableMark("lb__guying-turn")
    for to, ids in pairs(list) do
      for _, id in ipairs(cards) do
        if ids and table.contains(ids, id) then
          table.insertIfNeed(tos, tonumber(to))
          break
        end
      end
    end
    --]]    
    room:moveCards({
      ids = table.reverse(cards),
      toArea = Card.DrawPile,
      moveReason = fk.ReasonPut,
      proposer = player,
      skillName = skel.name,
      moveVisible = true,
      drawPilePosition = 1,
    })

    local card = Fk:cloneCard("amazing_grace")
    card.skill = Fk.skills["lb__guying_amazing_grace_skill"]
    room:useCard{
      from = player,
      tos = {},
      card = card,
      extra_data = {lb__guying = tos},
    }
  end,
})
--失去过牌的都能拿五谷牌，所以不需要只保留最后的移动者
--[[
skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(skel.name, true) then
      for _, move in ipairs(data) do
        if move.from then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              return true
            end
          end
        end
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.DiscardPile and not table.contains(player.room.discard_pile, info.cardId) then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local list = player:getTableMark("lb__guying-turn")
    for _, move in ipairs(data) do
      if move.from then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
            if list[tostring(move.from.id)] == nil then
              list[tostring(move.from.id)] = {info.cardId}
            else
              table.insertIfNeed(list[tostring(move.from.id)], info.cardId)
            end
          end
        end
      end
      for _, info in ipairs(move.moveInfo) do
        if info.fromArea == Card.DiscardPile and not table.contains(room.discard_pile, info.cardId) then
          for to, ids in pairs(list) do
            if table.contains(ids, info.cardId) then
              table.removeOne(list[to], info.cardId)
              break
            end
          end
        end
      end
    end
    room:setPlayerMark(player, "lb__guying-turn", list)
    --p(list)
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  room:addSkill("#CenterArea")
  local list = {}
  --local ids = {}
  room.logic:getEventsByRule(GameEvent.MoveCards, 1, function(e)
    for _, move in ipairs(e.data) do
      for _, info in ipairs(move.moveInfo) do
        --table.insert(ids, info.cardId)
        if move.from and table.contains(room.discard_pile, info.cardId) and 
        (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) then
        --and not table.contains(ids, info.cardId) then --倒找，只找最后一次移动
          if list[tostring(move.from.id)] == nil then
            list[tostring(move.from.id)] = {info.cardId}
          else
            table.insertIfNeed(list[tostring(move.from.id)], info.cardId)
          end
        end
      end
    end
  end, nil, Player.HistoryTurn)
  room:setPlayerMark(player, "lb__guying-turn", list)
end)
--]]
skel:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)
return skel
